package cn.xianyang.org.service.impl;

import cn.xianyang.basic.exception.BusinessException;
import cn.xianyang.basic.service.impl.BaseServiceImpl;
import cn.xianyang.basic.util.BaiduAuditUtils;
import cn.xianyang.basic.util.MailSenderUtils;
import cn.xianyang.org.domain.Employee;
import cn.xianyang.org.domain.Shop;
import cn.xianyang.org.domain.ShopAuditLog;
import cn.xianyang.org.mapper.EmployeeMapper;
import cn.xianyang.org.mapper.ShopAuditLogMapper;
import cn.xianyang.org.mapper.ShopMapper;
import cn.xianyang.org.service.IEmployeeService;
import cn.xianyang.org.service.IShopService;
import cn.xianyang.org.vo.ShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xianyang
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 店铺入驻逻辑
     *  1.参数不能为空校验
     *  2.合法校验 @TODD 百度AI智能审核接口
     *  3.判断是否曾经已经注册过
     *      1.店铺不能被注册
     *      2.人是否被注册
     *          2.1 注册了到那时没有绑定店铺
     *          2.2 注册了斌且已经定了了店铺
     *  4.保存数据
     *      shop
     *      employee
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //参数空校验
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(admin.getUsername())
        ){
            throw new BusinessException("参数不可以为空");
        }
        //合法校验 ，每调用一次就会少一次
       /* if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("名字出现非法字符");
        }*/
        //图片审核
        /*String str = "http://123.207.27.208"+shop.getLogo();
        if (!BaiduAuditUtils.ImgCensor(str)){
            throw new BusinessException("图片不合法,请重新上传");
        }*/

        //判断是否已经注册过,先写整体出来，在反向写出细节
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if (shopTmp != null){
            throw new BusinessException("该店铺已被注册，请直接登录");
        }
        //该身份证是否已注册
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        if (empTmp != null && empTmp.getShop_id() != null){
            //注册过并且绑定了店铺
            throw new BusinessException("该用户已有运营店铺，请更换或者注册新用户");
        }
        //都没有异常的话就直接新增
        if (empTmp == null){
            /**
             * 在保存员工的时候，需要保存logininfo，不建议在这里写，应该jiang业务交给service
             * 业务的高度封装
             */
            /*employeeMapper.save(admin);*/
            employeeService.save(admin);
            empTmp = admin;
        }
        //保存数据
        shop.setAdminId(empTmp.getId());
        shopMapper.save(shop);

        empTmp.setShop_id(shop.getId());
        //修改shop中的admin_id
        employeeMapper.update(empTmp);
    }

    /**
     * 审核通过发送邮件
     * @param log
     */
    @Override
    public void pass(ShopAuditLog log) {
        //修改店铺为审核通过
        Shop shop = shopMapper.loadById(log.getShopId());
        System.out.println("shop==============="+shop);
        shop.setState(1);
        shopMapper.update(shop);
        //记录审核通过日志
        log.setAuditId(334L);
        shopAuditLogMapper.save(log);
        //发送激活邮件 (给店长发)
        /*String email = employeeMapper.loadById(shop.getAdminId()).getEmail();
        MailSenderUtils.simpleSend(email,"你好，你的店铺信息已经通过审核，请尽快完成激活并使用:",
                "激活链接:http://localhost:8080/shop/activation/"+log.getShopId());*/
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        System.out.println("======================employee"+employee);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("806779279@qq.com");//发送
        message.setTo(employee.getEmail());//发送邮件
        message.setSubject("宠物乐园-店铺待激活");//标题
        message.setText("你好，你的店铺信息已经通过审核，请尽快完成激活并使用:"+
                "激活链接:http://localhost:8080/shop/activation/"+log.getShopId());
        javaMailSender.send(message);

    }

    /**
     * 驳回
     * @param log
     * 流程
     *    1.修改店铺状态
     *    2.新增日志 记录shopid 操作员 note 本次操作日志状态
     *    3.发送修改邮件
     *    一个.vue组件 传递shopid
     */
    @Override
    public void reject(ShopAuditLog log) {
        System.out.println(log.getShopId());
        //修改为驳回
        Shop shop = shopMapper.loadById(log.getShopId());
        shop.setState(2);
        shopMapper.update(shop);
        //记录审核未通过日志
        log.setAuditId(334L); //设置操作员 ID 写死的
        log.setState(2);
        shopAuditLogMapper.save(log);
        //发送修改链接
        /*String email = employeeMapper.loadById(shop.getAdminId()).getEmail();
        MailSenderUtils.simpleSend(email,
                "宠物之家-尊敬的用户,您的店铺审核未通过,请点击下方链接进行修改信息",
                "http://localhost:8081/#/register2");*//* @TODO 修改网页没有实现 */

        Employee employee = employeeMapper.loadById(shop.getAdminId());
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("806779279@qq.com");//发送
        message.setTo(employee.getEmail());//发送邮件
        message.setSubject("宠物乐园-店铺驳回待修改");//标题
        message.setText("你好，你的店铺信息有误，审核不能通过！！！请尽快修改并重新提交！"+
                "修改链接:http://localhost:8081/#/register2?shopId="+log.getShopId());

    }

    @Override
    public List<ShopVo> queryEcharts() {
        return shopMapper.queryEcharts();
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }
    /**
     * 店铺激活
     */
    @Override
    public void activation(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(4);
        shopMapper.update(shop);
    }

}
